package com.mkjb.service.impl;

import com.mkjb.dto.CustomerFormDto;
import com.mkjb.dto.CustomerListDto;
import com.mkjb.entity.Customer;
import com.mkjb.repository.CustomerMapper;
import com.mkjb.service.CustomerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/2/19.
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper mapper;

    @Override
    public void getList(CustomerListDto listDto) {
        List<Customer> list = mapper.getList(listDto);
        listDto.addAllList(list);

    }

    @Override
    public CustomerFormDto loadCustomerFromDto(String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            return new CustomerFormDto();
        }
        Customer customer = mapper.findByUuid(uuid);
        return new CustomerFormDto(customer);
    }

    @Override
    public void edit(CustomerFormDto formDto) {
        String uuid = formDto.getUuid();
        if (StringUtils.isEmpty(uuid)) {
            Customer customer = new Customer();
            formDto.update(customer);
            mapper.save(customer);
        } else {
            Customer customer = mapper.findByUuid(uuid);
            formDto.update(customer);
            mapper.update(customer);
        }
    }

    @Override
    public void delete(String uuid) {
        mapper.delete(uuid);
    }
}
